/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SpelerInschrijven.java
 *
 * Created on 2-jan-2012, 12:42:32
 */
package view;

import javax.swing.JOptionPane;
import model.Masterclass;
import model.Speler;
import model.MasterclassInschrijving;

/**
 *
 * @author tomkeim
 */
public class SpelerInschrijven extends javax.swing.JFrame {

    Speler speler;
    Masterclass masterclass;

    /** Creates new form SpelerInschrijven */
    public SpelerInschrijven() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jLabel1 = new javax.swing.JLabel();
        idField = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        selectedMasterclassField = new javax.swing.JTextField();
        zoekButton = new javax.swing.JButton();
        jSeparator2 = new javax.swing.JSeparator();
        selectedSpelerField = new javax.swing.JTextField();
        jSeparator3 = new javax.swing.JSeparator();
        spelerZoekButton = new javax.swing.JButton();
        spelerIdField = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        addSpelerToMasterclass = new javax.swing.JButton();
        heeftBetaald = new javax.swing.JCheckBox();

        jList1.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane1.setViewportView(jList1);

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Speler aan masterclass toevoegen");

        jLabel1.setText("Masterclass ID:");

        jLabel2.setText("Geselecteerde masterclass:");

        selectedMasterclassField.setText("Geen");
        selectedMasterclassField.setEnabled(false);

        zoekButton.setText("Zoek");
        zoekButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                zoekButtonActionPerformed(evt);
            }
        });

        selectedSpelerField.setText("Geen");
        selectedSpelerField.setEnabled(false);

        spelerZoekButton.setText("Zoek");
        spelerZoekButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                spelerZoekButtonActionPerformed(evt);
            }
        });

        jLabel3.setText("Geselecteerde speler:");

        jLabel4.setText("Speler ID:");

        addSpelerToMasterclass.setText("Speler aan masterclass toevoegen");
        addSpelerToMasterclass.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addSpelerToMasterclassActionPerformed(evt);
            }
        });

        heeftBetaald.setText("Speler heeft betaald");
        heeftBetaald.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                heeftBetaaldActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(heeftBetaald)
                        .addContainerGap())
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                        .add(layout.createSequentialGroup()
                            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                .add(layout.createSequentialGroup()
                                    .add(jLabel1)
                                    .add(18, 18, 18)
                                    .add(idField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 321, Short.MAX_VALUE)
                                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                    .add(zoekButton))
                                .add(layout.createSequentialGroup()
                                    .add(jLabel2)
                                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                    .add(selectedMasterclassField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 338, Short.MAX_VALUE))
                                .add(jSeparator2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 474, Short.MAX_VALUE)
                                .add(layout.createSequentialGroup()
                                    .add(jLabel4)
                                    .add(18, 18, 18)
                                    .add(spelerIdField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 347, Short.MAX_VALUE)
                                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                    .add(spelerZoekButton))
                                .add(layout.createSequentialGroup()
                                    .add(jLabel3)
                                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                    .add(selectedSpelerField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 365, Short.MAX_VALUE))
                                .add(jSeparator3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 474, Short.MAX_VALUE))
                            .addContainerGap())
                        .add(layout.createSequentialGroup()
                            .add(addSpelerToMasterclass, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 461, Short.MAX_VALUE)
                            .add(23, 23, 23)))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(idField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(zoekButton))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel2)
                    .add(selectedMasterclassField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(2, 2, 2)
                .add(jSeparator2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(spelerIdField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(spelerZoekButton)
                    .add(jLabel4))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel3)
                    .add(selectedSpelerField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(2, 2, 2)
                .add(jSeparator3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(4, 4, 4)
                .add(heeftBetaald)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(addSpelerToMasterclass)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void zoekButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_zoekButtonActionPerformed
        try {
            int id = Integer.parseInt(idField.getText());

            masterclass = Masterclass.getMasterclass(id);
            if (masterclass.getMasterclassID() > 0) {
                selectedMasterclassField.setText("#" + masterclass.getMasterclassID() + " op " + masterclass.getDatumTijd());

            } else {
                JOptionPane.showMessageDialog(null, "Masterclass niet gevonden", "Fout", JOptionPane.ERROR_MESSAGE);
                masterclass = null;
                selectedMasterclassField.setText("Geen");
            }
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(null, "Masterclassid moet geheel nummer zijn", "Fout", JOptionPane.ERROR_MESSAGE);
            masterclass = null;
            selectedMasterclassField.setText("Geen");
        }
    }//GEN-LAST:event_zoekButtonActionPerformed

    private void spelerZoekButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_spelerZoekButtonActionPerformed
        try {
            int id = Integer.parseInt(spelerIdField.getText());

            speler = Speler.getSpeler(id);
            if (speler.getSpelerid() > 0) {
                selectedSpelerField.setText("#" + speler.getSpelerid() + " met naam: " + speler.getNaam());

            } else {
                JOptionPane.showMessageDialog(null, "Speler niet gevonden", "Fout", JOptionPane.ERROR_MESSAGE);
                speler = null;
                selectedSpelerField.setText("Geen");
            }
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(null, "Speler id moet geheel nummer zijn", "Fout", JOptionPane.ERROR_MESSAGE);
            speler = null;
            selectedSpelerField.setText("Geen");
        }
    }//GEN-LAST:event_spelerZoekButtonActionPerformed

    private void addSpelerToMasterclassActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addSpelerToMasterclassActionPerformed
        if (masterclass != null) {
            if (speler != null) {
                if (masterclass.getMinimaleRating() > speler.getRating()) {
                    JOptionPane.showMessageDialog(null, "Deze speler heeft een te lage rating, en kan niet meedoen met deze masterclass.", "Fout", JOptionPane.ERROR_MESSAGE);
                } else {
                    MasterclassInschrijving inschrijving = MasterclassInschrijving.getInschrijving(masterclass, speler);
                    if (inschrijving.getMasterclass() == null) {
                        inschrijving = new MasterclassInschrijving();
                        inschrijving.setMasterclass(masterclass);
                        inschrijving.setSpeler(speler);
                        inschrijving.setHeeftBetaald(heeftBetaald.isSelected());
                        if (inschrijving.insert() == true) {
                            JOptionPane.showMessageDialog(null, "De speler is bij de masterclass ingeschreven.", "Gelukt!", JOptionPane.INFORMATION_MESSAGE);
                            this.dispose();
                        } else {
                            JOptionPane.showMessageDialog(null, "Er is een fout opgetreden tijdens het toevoegen!", "Fout", JOptionPane.ERROR_MESSAGE);
                        }
                    } else {
                        JOptionPane.showMessageDialog(null, "Deze speler is al insgeschreven.", "Fout", JOptionPane.ERROR_MESSAGE);
                    }
                }
            } else {
                JOptionPane.showMessageDialog(null, "U moet wel een speler kiezen.", "Fout", JOptionPane.ERROR_MESSAGE);
            }
        } else {
            JOptionPane.showMessageDialog(null, "U moet wel een masterclass kiezen.", "Fout", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_addSpelerToMasterclassActionPerformed

private void heeftBetaaldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_heeftBetaaldActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_heeftBetaaldActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(SpelerInschrijven.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(SpelerInschrijven.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(SpelerInschrijven.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(SpelerInschrijven.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new SpelerInschrijven().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addSpelerToMasterclass;
    private javax.swing.JCheckBox heeftBetaald;
    private javax.swing.JTextField idField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JList jList1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JTextField selectedMasterclassField;
    private javax.swing.JTextField selectedSpelerField;
    private javax.swing.JTextField spelerIdField;
    private javax.swing.JButton spelerZoekButton;
    private javax.swing.JButton zoekButton;
    // End of variables declaration//GEN-END:variables
}
